﻿using System.Web.Mvc;
using System.Web.UI;
using Liber.WEB;
using Liber;

namespace SoBusiness.Controllers
{
    public class MyBizController : LiberController
    {
        /// <summary>
        /// 个人工作桌面
        /// </summary>
        /// <returns></returns>
        public ViewResult Dashbord()
        {
            return View();
        }

        public ViewResult Find(string key="")
        {
            return View();
        }

        /// <summary>
        /// 个人设置首页
        /// </summary>
        /// <returns></returns>
        public ViewResult Index()
        {
            return View();
        }

        [OutputCache(NoStore=true,Location =OutputCacheLocation.None)]
        [WxSession]
        [NoSession]
        public ViewResult WxIndex()
        {
            return View();
        }

        /// <summary>
        /// 我的个性化导航栏和桌面部件设置页面
        /// </summary>
        /// <returns></returns>
        public ViewResult MyNavi()
        {
            return View();
        }

        /// <summary>
        /// 个人消息中心
        /// </summary>
        /// <returns></returns>
        public ViewResult MyMessage()
        {
            return View();
        }

        public ViewResult BindWechat()
        {
            return View();
        }

        public ViewResult AlterHeadimg()
        {
            return View();
        }

        public ViewResult AlterPwd()
        {
            return View();
        }

        public ViewResult AlterPhone()
        {
            return View();
        }

        public ViewResult AlterMail()
        {
            return View();
        }

        public ViewResult AlterCertify()
        {
            return View();
        }

        public ViewResult AddCompany()
        {
            return View();
        }

        /// <summary>
        /// 此页面部件用于设置用户的快捷导航和桌面部件
        /// </summary>
        /// <returns></returns>
        [ChildActionOnly]
        public PartialViewResult pvMyNavi()
        {
            return PartialView();
        }

        public JsonResult ApplyCertify(string phone,string name,string mail,string socialid,string idcardface)
        {
            var ac = API.Request("MineApp@ApplyAcntCertify")
                      .Set("phone", phone)
                      .Set("name", name)
                      .Set("mail", mail)
                      .Set("socialid", socialid)
                      .Set("idcardface", idcardface)
                      .WaitResult();
            return Json(ac);
        }

        [NoSession]
        /// <summary>
        /// 获取当前会话中的用户信息
        /// </summary>
        /// <returns></returns>
        public JsonResult GetUserInfo()
        {
            if (!IsLogined(HttpContext))
            {
                return Json(FnResult.Failure("MyBiz", "Unlogin"));
            }
            long uid = (long)Session["uid"];
            var ui = API.Request("MineApp@GetUserInfo")
                    .Set("acntid", uid)
                    .WaitResult();
            return Json(ui);
        }

        public JsonResult UpdateAcntSetting(string setting)
        {     
            string ns = setting;
            var r = API.Request("MineApp@UpdateSetting")
                     .Set("setting", ns)
                     .WaitResult();
            return Json(r);
        }

        public JsonResult ChangePwd(string oldpwd, string newpwd)
        {
            return Json(API.Request("MineApp@ChangePwd")
                           .Set("oldpwd", oldpwd)
                           .Set("newpwd", newpwd)
                           .WaitResult());
        }

        public JsonResult ChangePhone(string oldphone,string newphone)
        {
            return Json(API.Request("MineApp@UpdateAcnt")
                           .Set("newphone", newphone)
                           .WaitResult());
        }

        public JsonResult UpdateHeadimg(string headimg)
        {
            string ih = Server.UrlDecode(headimg);
            return Json(API.Request("MineApp@UpdateAcnt")
                           .Set("headimg", ih)
                           .WaitResult()
                           );
        }

        public JsonResult GetCertification()
        {
            return Json(API.Request("MineApp@GetCertification")
                           .WaitResult());
        }

        public JsonResult GetMyAlerts()
        {
            return Json(FnResult.Failure("MyBiz", "Unimplement"));
        }

        public JsonResult GetMyTodo()
        {
            var rtn = API.Request("TodoSvc@GetMyTODO")
                       .WaitResult();
            return Json(rtn);
        }

        public JsonResult GetAlertCount()
        {
            return Json(FnResult.Failure("MyBiz", "Umimplement"));
        }

        public JsonResult BindOpenUser(string src,string name,string sex,string headimg,string id)
        {
            return Json(API.Request("MineApp@BindOpenUser")
                           .Set("opensrc", src)
                           .Set("openname", name)
                           .Set("opensex", sex)
                           .Set("openheadimg", headimg)
                           .Set("openid", id)
                           .WaitResult());
        }

        public JsonResult UnbindWechat()
        {
            return Json(API.Request("AccountSvc@UnbindOpenuser")
                           .Set("opensrc", "wechat")
                           .WaitResult());
        }

        public JsonResult GetMyExCompanies()
        {
            return Json(API.Request("OrganizeSvc@GetAcntExCompanies").WaitResult());
        }
    }
}